﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common.Utility.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// 扩展方法,根据指定字符串分隔字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string[] Split(this string str, string separator)
        {
            return str.Split(separator, StringSplitOptions.None);
        }

        /// <summary>
        /// 扩展方法,根据指定字符串分隔字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="separator"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string[] Split(this string str, string separator, StringSplitOptions options)
        {
            return str.Split(new string[] { separator }, options);
        }

        /// <summary>
        /// 扩展方法,截取指定字节长度的字符串,当截取长度大于待截取字符串长度时不会报异常,返回待截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string SubByteString(this string str, int len)
        {
            byte[] buffer = System.Text.Encoding.Default.GetBytes(str);

            if (buffer.Length > len)
            {
                str = System.Text.Encoding.Default.GetString(buffer, 0, len);
            }
            return str.TrimEnd('?');
        }

        /// <summary>
        /// 扩展方法,截取指定长度的字符串,当截取长度大于待截取字符串长度时不会报异常,返回待截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string SubString(this string str, int len)
        {
            if (str.Length > len) return str.Substring(len);
            return str;
        }

        /// <summary>
        /// 扩展方法,将指定的字符串转换为首字母大写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToTitleCase(string str)
        {
            return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str);
        }

        /// <summary>
        /// 高效字符串拼接实现(比直接使用string.Format()方法高效)。
        /// </summary>
        /// <param name="format">复合格式字符串。</param>
        /// <param name="args"> 一个对象数组，其中包含零个或多个要设置格式的对象。</param>
        /// <returns></returns>
        public static string Format(this string format, params object[] args)
        {
            if (format == null || args == null)
                throw new ArgumentNullException((format == null) ? "format" : "args");

            var capacity = format.Length + args.Where(p => p != null).Select(p => p.ToString()).Sum(p => p.Length);
            var stringBuilder = new StringBuilder(capacity);
            stringBuilder.AppendFormat(format, args);
            return stringBuilder.ToString();
        }
    }
}
